package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.CommentService;
import com.pro.utils.DateUtils;
import com.pro.utils.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    
    @Autowired
    private CommentService commentService;

    SensitiveWordsUtil swu = new SensitiveWordsUtil();

    //获取评论个数
    @RequestMapping("/countCommentNum")
    @ResponseBody
    public JsonData countCommentNum(String type, String userType, String status){
        List<JsComment> list = new ArrayList<>();
        JsComment comment = new JsComment();
        comment.setAllNum(commentService.countCommentNum(type,userType,"",""));
        comment.setNormalNum(commentService.countCommentNum(type,userType,"1",""));
        comment.setInterceptNum(commentService.countCommentNum(type,userType,"2",""));
        comment.setDeleteNum(commentService.countCommentNum(type,userType,"3",""));
        comment.setHistoryNum(commentService.countCommentNum(type,userType,status,""));
        comment.setHistoryPeople(commentService.countCommentPeople(type,userType,status,""));

        String date = new DateUtils().tranferDate(new Date());
        comment.setTodayNum(commentService.countCommentNum(type,userType,status,date));
        comment.setTodayPeople(commentService.countCommentPeople(type,userType,status,date));
        list.add(comment);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //拦截、恢复评论
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonData updateStatus(String status, Integer id){
        boolean b = commentService.updateStatus(status, id);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //删除评论
    @RequestMapping("/deleteComment")
    @ResponseBody
    public JsonData deleteComment(Integer id){
        boolean b = commentService.deleteComment(id);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //获取个人商品评论个数
    @RequestMapping("/personalProductNum")
    @ResponseBody
    public JsonData personalProductNum(Integer createId, String type, String userType, String status, String commentDate){
        List<JsComment> list = new ArrayList<>();
        JsComment comment = new JsComment();
        comment.setAllNum(commentService.personalProductNum(createId,type,userType,"",""));
        comment.setNormalNum(commentService.personalProductNum(createId,type,userType,"1",""));
        comment.setInterceptNum(commentService.personalProductNum(createId,type,userType,"2",""));
        comment.setDeleteNum(commentService.personalProductNum(createId,type,userType,"3",""));
        comment.setHistoryNum(commentService.personalProductNum(createId,type,userType,status,""));
        comment.setHistoryPeople(commentService.personalProductPeople(createId,type,userType,status,""));

        String date = new DateUtils().tranferDate(new Date());
        comment.setTodayNum(commentService.personalProductNum(createId,type,userType,status,date));
        comment.setTodayPeople(commentService.personalProductPeople(createId,type,userType,status,date));
        list.add(comment);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取个人课程评论个数
    @RequestMapping("/personalCourseNum")
    @ResponseBody
    public JsonData personalCourseNum(Integer createId, String type, String userType, String status, String commentDate){
        List<JsComment> list = new ArrayList<>();
        JsComment comment = new JsComment();
        comment.setAllNum(commentService.personalCourseNum(createId,type,userType,"",""));
        comment.setNormalNum(commentService.personalCourseNum(createId,type,userType,"1",""));
        comment.setInterceptNum(commentService.personalCourseNum(createId,type,userType,"2",""));
        comment.setDeleteNum(commentService.personalCourseNum(createId,type,userType,"3",""));
        comment.setHistoryNum(commentService.personalCourseNum(createId,type,userType,status,""));
        comment.setHistoryPeople(commentService.personalCoursePeople(createId,type,userType,status,""));

        String date = new DateUtils().tranferDate(new Date());
        comment.setTodayNum(commentService.personalCourseNum(createId,type,userType,status,date));
        comment.setTodayPeople(commentService.personalCoursePeople(createId,type,userType,status,date));
        list.add(comment);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取个人微圈评论个数
    @RequestMapping("/personalMicrocircleNum")
    @ResponseBody
    public JsonData personalMicrocircleNum(Integer userId, String type, String userType, String status, String commentDate){
        List<JsComment> list = new ArrayList<>();
        JsComment comment = new JsComment();
        comment.setAllNum(commentService.personalMicrocircleNum(userId,type,userType,"",""));
        comment.setNormalNum(commentService.personalMicrocircleNum(userId,type,userType,"1",""));
        comment.setInterceptNum(commentService.personalMicrocircleNum(userId,type,userType,"2",""));
        comment.setDeleteNum(commentService.personalMicrocircleNum(userId,type,userType,"3",""));
        comment.setHistoryNum(commentService.personalMicrocircleNum(userId,type,userType,status,""));
        comment.setHistoryPeople(commentService.personalMicrocirclePeople(userId,type,userType,status,""));

        String date = new DateUtils().tranferDate(new Date());
        comment.setTodayNum(commentService.personalMicrocircleNum(userId,type,userType,status,date));
        comment.setTodayPeople(commentService.personalMicrocirclePeople(userId,type,userType,status,date));
        list.add(comment);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取前端用户商品评论
    @RequestMapping("/getProductUserTable")
    @ResponseBody
    public JsonData getUserCommentTable(Integer createId,String status){
        List<JsComment> list = commentService.getUserProductComments(createId,status);
        list = swu.getListFilter(list);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取管理人员商品评论
    @RequestMapping("/getProductTable")
    @ResponseBody
    public JsonData getAllCommentTable(Integer createId,String status){
        List<JsComment> list = commentService.getProductComments(createId,status);
        list = swu.getListFilter(list);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取前端用户课程评论
    @RequestMapping("/getCourseUserTable")
    @ResponseBody
    public JsonData getCourseUserCommentTable(Integer createId,String status){
        List<JsComment> list = commentService.getUserCourseComments(createId,status);
        list = swu.getListFilter(list);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取管理人员课程评论
    @RequestMapping("/getCourseTable")
    @ResponseBody
    public JsonData getCourseCommentTable(Integer createId,String status){
        List<JsComment> list = commentService.getCourseComments(createId,status);
        list = swu.getListFilter(list);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取前端用户微圈评论
    @RequestMapping("/getMicrocircleUserTable")
    @ResponseBody
    public JsonData getUserMicrocircleComments(Integer userId,String status){
        List<JsComment> list = commentService.getUserMicrocircleComments(userId,status);
        list = swu.getListFilter(list);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //获取管理人员微圈评论
    @RequestMapping("/getMicrocircleTable")
    @ResponseBody
    public JsonData getMicrocircleComments(Integer userId,String status){
        List<JsComment> list = commentService.getMicrocircleComments(userId,status);
        list = swu.getListFilter(list);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }
    //添加一条微圈评论
    @RequestMapping("/addMicrocircleComment")
    @ResponseBody
    public JsonData addMicrocircleComment(JsComment layForm){
        boolean b = commentService.addMicrocircleComment(layForm);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
    //对状态进行修改
    @RequestMapping("/updateDisplay")
    @ResponseBody
    public JsonData updateDisplay(JsComment param){
        boolean b = commentService.updateDisplay(param);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
    //查询类型为后端的维权评论
    @RequestMapping("/getTypeComment")
    @ResponseBody
    public JsonData getTypeComment(){
        List<JsComment> typeComment = commentService.getTypeComment();
        List<JsComment> listFilter = SensitiveWordsUtil.getListFilter(typeComment);
        return listFilter!=null?JsonData.buildSuccess(listFilter):JsonData.buildError("服务器出错了");
    }
    //查看商品详情
    @RequestMapping("/getProductInformation")
    @ResponseBody
    public JsonData getProductInformation(Integer id){
        Product product = commentService.getProductInformation(id);
        return product != null?JsonData.buildSuccess(product):JsonData.buildError("服务器出错了");
    }

    //查看课程详情
    @RequestMapping("/getCourseInformation")
    @ResponseBody
    public JsonData getCourseInformation(Integer id){
        JsCourse course = commentService.getCourseInformation(id);
        return course != null?JsonData.buildSuccess(course):JsonData.buildError("服务器出错了");
    }

    //查看文章详情
    @RequestMapping("/getMicrocircleInformation")
    @ResponseBody
    public JsonData getMicrocircleInformation(Integer id){
        JsMicrocircle microcircle = commentService.getMicrocircleInformation(id);
        return microcircle != null?JsonData.buildSuccess(microcircle):JsonData.buildError("服务器出错了");
    }

    //查看所有评论
    @RequestMapping("/getALLComment")
    @ResponseBody
    public JsonData getAllComment(Integer commodityId,Integer type){
        List<JsComment> list = commentService.getAllComment(commodityId,type);
        List<JsComment> listBefore = commentService.getAllBeforeComment(commodityId, type);
        for(JsComment comment : listBefore){
            list.add(comment);
        }
        list = swu.getListFilter(list);
        return list != null?JsonData.buildSuccess(list):JsonData.buildError("服务器出错了");
    }

    //提交评论
    @RequestMapping("/submitComment")
    @ResponseBody
    public JsonData submitComment(Integer userId,Integer commodityId,Integer parentId,String content,Integer type,String userType){
        String date = new DateUtils().tranferDateTime(new Date());
        boolean b = commentService.submitComment(userId, commodityId, parentId, content, date, 2,type,userType);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }

    //隐藏评论
    @RequestMapping("/showComment")
    @ResponseBody
    public JsonData showComment(Integer id,Integer display){
        boolean b = commentService.showComment(id, display);
        return b?JsonData.buildSuccess(b):JsonData.buildError("服务器出错了");
    }
}
